package org.wlw.teacher.servlet;


import org.wlw.teacher.anno.Resource;
import org.wlw.teacher.models.Department;
import org.wlw.teacher.models.Pager;
import org.wlw.teacher.service.IDepartmentService;
import org.wlw.teacher.utils.Contrants;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/admin/dep")
public class DepartmentServlet extends BaseServlet{
    private IDepartmentService departmentService;

    @Resource
    public void setDepartmentService(IDepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    public String index(HttpServletRequest request, HttpServletResponse response) {
        //获取请求参数
        Department department = new Department();
        String name = request.getParameter("name");
        String type = request.getParameter("type");
        if(name!=null&&!"".equals(name)) {
            department.setName(name);
        }
        if(type!=null&&!"".equals(type)) {
            department.setType(type);
        }
        request.setAttribute("searchT",department);
        //以上设置了参数

        Pager<Department> depList = departmentService.find(department);
        request.setAttribute("types",Contrants.DEP_TYPE);
        handleColumns(depList.getData());
        request.setAttribute("datas",depList);
        return "dep/list";
    }

    private void handleColumns(List<Department> depList) {
        for (Department dep:depList) {
            dep.setTypeDesc(Contrants.DEP_TYPE.get(dep.getType()));
        }
    }

    public String delete(HttpServletRequest request,HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        departmentService.delete(id);
        return this.getRedirectUrl("/admin/dep");
    }

    public String add(HttpServletRequest request,HttpServletResponse response) {
        request.setAttribute("types",Contrants.DEP_TYPE);
        return "/dep/add";
    }

    public String update(HttpServletRequest request,HttpServletResponse response) {
        request.setAttribute("types",Contrants.DEP_TYPE);
        Integer id = Integer.parseInt(request.getParameter("id"));
        //根据id获取Dep对象
        Department d = departmentService.load(id);
        request.setAttribute("dep",d);
        return "/dep/update";
    }

    public String addAction(HttpServletRequest request,HttpServletResponse response) {
        String name = request.getParameter("name");
        String type = request.getParameter("type");
        String phone = request.getParameter("phone");
        Department d = new Department();
        d.setPhone(phone);
        d.setName(name);
        d.setType(type);
        departmentService.add(d);
        return getRedirectUrl("/admin/dep");
    }

    public String updateAction(HttpServletRequest request,HttpServletResponse response) {
        String name = request.getParameter("name");
        String type = request.getParameter("type");
        String phone = request.getParameter("phone");
        int id = Integer.parseInt(request.getParameter("id"));
        Department dep = departmentService.load(id);
        dep.setType(type);
        dep.setPhone(phone);
        dep.setName(name);
        departmentService.update(dep);
        return getRedirectUrl("/admin/dep");
    }
}
